// http.js文件,记得在main.js用import一下该文件，
// 要不然该文件都没地方被引入；import Vue from 'vue' 也不会生效
import axios from 'axios';
import router from '@/router'
import { Message } from 'element-ui'
import Vue from 'vue'
import CONST from "@/const/global";
import vuexApi from "@/api/vuexApi";
import axiosApi from "@/api/axiosApi";
import sessionApi from "@/api/sessionApi";


Vue.prototype.$get = get
Vue.prototype.$put = put
Vue.prototype.$post = post
Vue.prototype.$del = del
Vue.prototype.$CONST = CONST
Vue.prototype.$vuexApi = vuexApi
Vue.prototype.$axiosApi = axiosApi;


// 2. 引入已经实例化后的router的实例，做路由跳转，比如登录拦截、401重定向登录页面


const qs = require('qs');

let flag = false

// 6秒超时
axios.defaults.timeout = 6000;
axios.defaults.baseURL = "/dev";
axios.defaults.withCredentials = true;


//http 请求 拦截器
axios.interceptors.request.use(
	config => {
		// 请求之前都要从localstorage中取出token带上
		let token = sessionApi.getToken()
		if (token) {
			config.headers.common['token'] = token
		}
		return config;
	},
	error => {
		return Promise.reject(error);
	}
);


//http 响应 拦截器
axios.interceptors.response.use(

	response => {

		//如果返回的是图片类型，那就是后端没有用Response对象包装过的，直接返回
		let contentType = response.headers['content-type']
		if (contentType && contentType.indexOf('image') !== -1) {
			return response
		}

		//格式错误的形式
		let responseData = response.data;
		if (responseData.code === undefined
			|| responseData.code === null) {
			Message({
				message: "",
				type: "error",
				duration: 3000
			});
			return Promise.reject(responseData);
		}

		// 200 正常，直接返回数据
		if (responseData.code === "200") {
			return response.data;
		}


		//请先登录,加入一个flag，避免多个接口同时没有权限提醒
		if (responseData.code === "602") {
			if (!flag) {
				flag = true;
				router.push({path: '/login', query: {redirect: router.currentRoute.fullPath}}).then();
				Message({
					message: responseData.msg,
					type: "error",
					duration: 1000,
					onClose: () => {
						//flag值复原
						flag = false
					}
				});
			}
			return Promise.reject(responseData)
		}

		Message({
			message: responseData.msg,
			type: "error",
			duration: 3000
		});

		return Promise.reject(responseData)
	},
	error => {
		Message({
			message: error,
			type: "error",
			duration: 3000
		});
		return Promise.reject(error);
	}
);


/**
 * 封装get方法
 * @param url
 * @param data
 * @返回s {Promise}
 */
export function get(url, params = {}) {
	return axios.get(url, {
		params: params,
		//用来处理前端传数组到后端
		paramsSerializer:params=>{
			return qs.stringify(params,{indices:false})
		}
	})
}

//qs.stringify()将数组转换成后端list
//qs.stringify({ ids: [1, 2] }, { arrayFormat: 'indices' }) // ids[0]=1&ids[1]=2
//qs.stringify({ ids: [1, 2] }, { arrayFormat: 'brackets' }) // ids[]=1&ids[]=2
//qs.stringify({ ids: [1, 2] }, { arrayFormat: 'repeat' }) // ids=1&ids=2


/**
 * 封装post请求
 * @param url
 * @param data
 * @返回s {Promise}
 */
export function post(url, data = {}) {
	return axios.post(url,data);
}


/**
 * 封装put请求
 * @param url
 * @param data
 * @返回s {Promise}
 */
export function put(url, data={}) {
	return axios.put(url,data)
}


/**
 * @描述  del 方法，用显式方式提交
 * @返回  promise对象
 * @码农 胖丁
 * @日期 2021/1/16 11:28
 */
export function del(url,params = {}){
	return axios.delete(url, {
		params
	})
}


